import javax.swing.*;
import java.awt.event.*;

public class NetDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField function;
    private JTextField params;
    private JTextField netAddress;
    private JTextField dataType;
    private JTextField interfaceFunction;
    private JTextField moduleName;

    public interface DialogCallBack{

        void ok(String moduleName ,String functionName, String paramsName, String netAddressName, String dataTypeName, String interfaceFunctionName);
    }
    private final DialogCallBack mCallback;
    public NetDialog(DialogCallBack mCallback) {
        this.mCallback =mCallback;
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        setSize(600, 300);
        setLocationRelativeTo(null);
        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        /**
         * 获取我们填写的参数
         */

        String modulename = moduleName.getText().toString();
        String functionName = function.getText().toString();
        String paramsName = params.getText().toString();
        String netAddressName = netAddress.getText().toString();
        String interfaceFunctionName = interfaceFunction.getText().toString();
        String dataTypeName = dataType.getText().toString();
        if (mCallback!=null){
            mCallback.ok( modulename,functionName,paramsName,netAddressName,dataTypeName,interfaceFunctionName);


        }

        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    public static void main(String[] args) {
        NetDialog dialog = new NetDialog(new DialogCallBack() {
            @Override
            public void ok(String name ,String functionName, String paramsName, String netAddressName, String dataTypeName, String interfaceFunctionName) {

            }


        });
        dialog.pack();
        dialog.setVisible(true);
        System.exit(0);
    }
}
